﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace VQWEB.common
{
    public class StringHelper
    {       
        interface IHtmlFormat
        {
            string Format(string data);
        }

        protected class RegexFormat : IHtmlFormat
        {
            private string _replace;
            private Regex _regex;

            public RegexFormat(string pattern, string replace)
                : this(pattern, replace, true)
            {

            }

            public RegexFormat(string pattern, string replace, bool ignoreCase)
            {
                RegexOptions options = RegexOptions.Compiled;

                if (ignoreCase)
                {
                    options |= RegexOptions.IgnoreCase;
                }

                _replace = replace;
                _regex = new Regex(pattern, options);
            }

            public string Format(string data)
            {
                return _regex.Replace(data, _replace);
            }
        }

        protected class Textplain : IHtmlFormat
        {
            private string _pattern;
            private string _replace;

            public Textplain(string pattern, string replace)
            {
                _pattern = pattern;
                _replace = replace;
            }

            public string Format(string data)
            {
                return data.Replace(_pattern, _replace);
            }
        }

        static List<IHtmlFormat> _formatters;

        static private void Init(int Id)
        {
            string key = "key" + Id ;
            _formatters = new List<IHtmlFormat>();
            _formatters.Add(new Textplain("\n", "<br />"));
            _formatters.Add(new RegexFormat(@"\{(.*?)}", "<input type=\"text\" name=" + key + "_$1 style=\"border-style:none; border-bottom-style:groove;\" />"));
            _formatters.Add(new RegexFormat(@"\[(.*?)]", "<input type=\"checkbox\" name=" + key + "_$1  value=\"$1\" />"));
            _formatters.Add(new RegexFormat(@"[(](.*?)[)]", "<input type=\"radio\" name=" + key + "_  value=\"$1\" />"));
        }        

        public static string Format(string data, int Id)
        {
            Init(Id);
            foreach (IHtmlFormat formatter in _formatters)
            {
                data = formatter.Format(data);
            }

            return data;
        }
       
    }
}
